package com.yanqu.road.server.pb;

import com.yanqu.road.entity.assistant.AssistantBodySkin;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.wingroom.*;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;
import com.yanqu.road.entity.wingroom.message.GuestMessage;
import com.yanqu.road.entity.wingroom.message.UserWingRoomMessageData;
import com.yanqu.road.entity.wingroom.message.WingRoomMessageDetailInfo;
import com.yanqu.road.entity.wingroom.workshop.UserWorkShopData;
import com.yanqu.road.entity.wingroom.workshop.WorkShopBuyCount;
import com.yanqu.road.entity.wingroom.workshop.WorkShopItem;
import com.yanqu.road.entity.wingroom.workshop.WorkShopPool;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.WingRoomChakraProto.WingRoomChakraProto;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author ：zfr
 * @date ：2022/2/8 15:27
 */
public class WingRoomPb {
    public static WingRoomFurniture parseFurniture(WingRoomProto.WingRoomFurnitureTemp temp) {
        WingRoomFurniture furniture = new WingRoomFurniture();
        furniture.setFurnitureId(temp.getFurnitureId());
        furniture.setFaceDirection(temp.getFaceDirection());
        furniture.setNo(temp.getNo());
        furniture.setOriginBlock(new RegionBlock(temp.getBlock().getX(), temp.getBlock().getY()));
        return furniture;
    }

    public static List<WingRoomFurniture> parseFurnitureList(List<WingRoomProto.WingRoomFurnitureTemp> tempList) {
        List<WingRoomFurniture> furnitureList = new ArrayList<>();
        for (WingRoomProto.WingRoomFurnitureTemp temp : tempList) {
            furnitureList.add(parseFurniture(temp));
        }
        return furnitureList;
    }

    public static WingRoomProto.UserWingRoomDataSyncMsg.Builder buildUserWingRoomDataMsg(UserWingRoomData userWingRoomData) {
        WingRoomProto.UserWingRoomDataSyncMsg.Builder syncMsg = WingRoomProto.UserWingRoomDataSyncMsg.newBuilder();
        syncMsg.setLv(userWingRoomData.getLv());
        syncMsg.setComfortValue(userWingRoomData.getComfortValue());
        syncMsg.setGetEventRewardTimes(userWingRoomData.getTotalGetEventRewardTimes());
        syncMsg.setRewardTimesResetTime(userWingRoomData.getTimesResetTime());
        syncMsg.setBgm(userWingRoomData.getBgm());
        return syncMsg;
    }

    public static WingRoomProto.WingRoomFurnitureTemp.Builder buildFurnitureTempMsg(WingRoomFurniture wingRoomFurniture) {
        WingRoomProto.WingRoomFurnitureTemp.Builder furnitureTemp = WingRoomProto.WingRoomFurnitureTemp.newBuilder();
        furnitureTemp.setFurnitureId(wingRoomFurniture.getFurnitureId());
        furnitureTemp.setFaceDirection(wingRoomFurniture.getFaceDirection());
        furnitureTemp.setNo(wingRoomFurniture.getNo());
        furnitureTemp.setIsRemove(false);
        WingRoomProto.BlockTemp.Builder blockTemp = WingRoomProto.BlockTemp.newBuilder();
        blockTemp.setX(wingRoomFurniture.getOriginBlock().getX());
        blockTemp.setY(wingRoomFurniture.getOriginBlock().getY());
        furnitureTemp.setBlock(blockTemp);
        return furnitureTemp;
    }

    public static WingRoomProto.WingRoomRegionTemp.Builder buildWingRoomRegionTempMsg(WingRoomRegion region) {
        WingRoomProto.WingRoomRegionTemp.Builder temp = WingRoomProto.WingRoomRegionTemp.newBuilder();
        temp.setRegionId(region.getRegionId());
        for (WingRoomFurniture furniture : region.getFurnitureList()) {
            temp.addFurniture(buildFurnitureTempMsg(furniture));
        }
        return temp;
    }

    public static WingRoomProto.UserFurnitureSampleTemp.Builder buildFurnitureSampleTempMsg(UserFurnitureSample sample) {
        WingRoomProto.UserFurnitureSampleTemp.Builder temp = WingRoomProto.UserFurnitureSampleTemp.newBuilder();
        temp.setFurnitureId(sample.getFurnitureId());
        temp.setLv(sample.getLv());
        return temp;
    }

    public static WingRoomProto.UserWingRoomPlanTemp.Builder buildPlanTempMsg(UserWingRoomPlan plan) {
        WingRoomProto.UserWingRoomPlanTemp.Builder temp = WingRoomProto.UserWingRoomPlanTemp.newBuilder();
        temp.setPlanUid(plan.getPlanUid());
        temp.setPlanName(plan.getPlanName());
        for (WingRoomRegion region : plan.getRegionMap().values()) {
            temp.addRegion(buildWingRoomRegionTempMsg(region));
        }

        return temp;
    }

    public static WingRoomProto.UserWingRoomShareEventTemp.Builder buildEventTempMsg(UserWingRoomShareEvent event) {
        WingRoomProto.UserWingRoomShareEventTemp.Builder shareEventTemp = WingRoomProto.UserWingRoomShareEventTemp.newBuilder();
        shareEventTemp.setStatus(event.getStatus());
        shareEventTemp.setLastUpdateTime(event.getLastUpdateTime());
        shareEventTemp.setTriggerTime(event.getTriggerTime());
        shareEventTemp.setGetRewardTime(event.getGetRewardTime());
        for (WingRoomEventReward reward : event.getRewardMap().values()) {
            shareEventTemp.addReward(buildEventRewardTempMsg(reward));
        }
        return shareEventTemp;
    }

    public static WingRoomProto.WingRoomEventRewardTemp.Builder buildEventRewardTempMsg(WingRoomEventReward reward) {
        WingRoomProto.WingRoomEventRewardTemp.Builder rewardTemp = WingRoomProto.WingRoomEventRewardTemp.newBuilder();
        rewardTemp.setPos(reward.getPos());
        rewardTemp.setIsGetReward(reward.isGetReward());
        if (reward.isGetReward()) {
            rewardTemp.setReward(reward.getReward());
            rewardTemp.setPlayerId(reward.getPlayerId());
            rewardTemp.setPlayerName(reward.getPlayerName());
            rewardTemp.setGetTime(reward.getGetTime());
        }
        return rewardTemp;
    }

    public static WingRoomProto.WatchOtherPlayerRespMsg.Builder buildWatchOtherRespMsg(UserWingRoomData data, UserWingRoomShareEvent event, AssistantBodySkin assistantBodySkin, UserWingRoomChakra userWingRoomChakra, Map<Integer, Map<Integer, Map<Integer, UserWingRoomChakraDetail>>> userWingRoomChakraDetailMap) {
        WingRoomProto.WatchOtherPlayerRespMsg.Builder respMsg = WingRoomProto.WatchOtherPlayerRespMsg.newBuilder();
        if (data == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_LOCK);
            return respMsg;
        }
        respMsg.setRet(0);
        respMsg.setLv(data.getLv());
        respMsg.setComfortValue(data.getComfortValue());
        String bgm = data.getBgm();
        if (bgm == null) {
            bgm = "0";
        }
        respMsg.setBgm(bgm);
        for (WingRoomRegion region : data.getRegionMap().values()) {
            respMsg.addRegion(buildWingRoomRegionTempMsg(region));
        }
        if (event != null) {
            respMsg.setEvent(buildEventTempMsg(event));
        }
        respMsg.setAssistant(AssistantSkinPb.parseAssistantBodyDecorationsMsg(assistantBodySkin));
        //命盘数据
        if (userWingRoomChakra != null) {
            WingRoomChakraProto.WingRoomChakraData.Builder chakraData = WingRoomChakraPb.buildChakraData(userWingRoomChakra, userWingRoomChakraDetailMap, null, 0);
            respMsg.setChakraData(chakraData);
            respMsg.setChakraOpenFlag(SystemOpenMgr.checkSystemOpen(data.getUserId(), eSystemId.Chakra.getValue()));
        }
        return respMsg;
    }

    public static WingRoomProto.FurnitureSuitDataTemp.Builder buildFurnitureSuitDataTemp(UserFurnitureSuitData suitData) {
        WingRoomProto.FurnitureSuitDataTemp.Builder temp = WingRoomProto.FurnitureSuitDataTemp.newBuilder();
        temp.setSuitType(suitData.getSuitType());
        temp.setLv(suitData.getSuitLv());
        return temp;
    }

    public static WingRoomProto.WorkShopBuyCountTemp.Builder buildWorkShopBuyCountTemp(WorkShopBuyCount buyCount) {
        WingRoomProto.WorkShopBuyCountTemp.Builder temp = WingRoomProto.WorkShopBuyCountTemp.newBuilder();
        temp.setFurnitureId(buyCount.getFurnitureId());
        temp.setBuyCount(buyCount.getBuyCount());
        return temp;
    }

    public static WingRoomProto.WingRoomFriendTemp.Builder buildWingRoomFriendTemp(long playerId, long userId, UserInfo userInfo, UserWingRoomShareEvent event, boolean isRelative) {
        WingRoomProto.WingRoomFriendTemp.Builder temp = WingRoomProto.WingRoomFriendTemp.newBuilder();
        temp.setUserId(userId);
        temp.setPlayerBaseInfo(PlayerPb.parsePlayerBaseTempMsg(userInfo));
        temp.setDurEvent(WingRoomMgr.isDuringEventShare(event, playerId) == 0);
        temp.setIsRelative(isRelative);
        return temp;
    }

    public static WingRoomProto.WingRoomFriendTemp.Builder buildWingRoomFriendTemp(long playerId, long userId, UserBaseInfo userInfo, UserWingRoomShareEvent event, boolean isRelative) {
        WingRoomProto.WingRoomFriendTemp.Builder temp = WingRoomProto.WingRoomFriendTemp.newBuilder();
        temp.setUserId(userId);
        temp.setPlayerBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userInfo));
        temp.setDurEvent(WingRoomMgr.isDuringEventShare(event, playerId) == 0);
        temp.setIsRelative(isRelative);
        return temp;
    }

    public static WingRoomProto.MessageAllDataMsg.Builder buildWingRoomMessageAllData(WingRoomMessageDetailInfo messageDetailInfo, boolean likeToday, List<WingRoomProto.MessageTempMsg.Builder> builders) {
        WingRoomProto.MessageAllDataMsg.Builder builder = WingRoomProto.MessageAllDataMsg.newBuilder();
        builder.setSwitchType(messageDetailInfo.getMessageSwitch());
        builder.setGiveLikes(messageDetailInfo.getSelfMessageGiveLikeTimes());
        builder.setOwnerMessageContent(messageDetailInfo.getSelfMessageContent());
        builder.setGiveLikeToday(likeToday);
        if (builders != null) {
            for (WingRoomProto.MessageTempMsg.Builder message : builders) {
                builder.addGuestMessage(message);
            }
        }
        return builder;
    }

    public static WingRoomProto.MessageTempMsg.Builder buildMessageTemp(GuestMessage message) {
        WingRoomProto.MessageTempMsg.Builder builder = WingRoomProto.MessageTempMsg.newBuilder();
        builder.setMessageId(message.getMessageId());
        builder.setIsTop(message.isTop());
        builder.setNoteStyle(message.getNoteStyle());
        builder.setIsRead(message.isRead());
        return builder;
    }

    public static WingRoomProto.UserSelfMessageDataMsg.Builder buildSelfMessageDataMsg(UserWingRoomMessageData data, int size, boolean redDot) {
        WingRoomProto.UserSelfMessageDataMsg.Builder msg = WingRoomProto.UserSelfMessageDataMsg.newBuilder();
        if (data == null) {
            msg.setRedDot(redDot);
            msg.setTodayGiveLikeTimes(0);
            msg.setTodayMessageTimes(0);
            return msg;
        }
        msg.setRedDot(redDot);
        msg.setTodayMessageTimes(data.getTodayMessageTimes() + data.getPreReduceMessageTimes());
        msg.setTodayGiveLikeTimes(size);
        return msg;
    }

    public static WingRoomProto.WorkShopPoolTemp.Builder buildWorkShopPoolTemp(WorkShopPool pool, int freshDays) {
        WingRoomProto.WorkShopPoolTemp.Builder temp = WingRoomProto.WorkShopPoolTemp.newBuilder();
        temp.setRefreshTime(pool.getRefreshTime() + TimeUnit.DAYS.toMillis(freshDays));
        for (WorkShopItem workShopItem : pool.getWorkShopItemList()) {
            temp.addWorkShopItem(buildWorkShopItemTemp(workShopItem));
        }
        return temp;
    }

    private static WingRoomProto.WorkShopItemTemp.Builder buildWorkShopItemTemp(WorkShopItem item) {
        WingRoomProto.WorkShopItemTemp.Builder temp = WingRoomProto.WorkShopItemTemp.newBuilder();
        temp.setFurnitureId(item.getFurnitureId());
        temp.setCount(item.getCount());
        return temp;
    }

    public static WorkShopPool parseWorkShopPool(WingRoomProto.WorkShopPoolTemp.Builder poolMsg) {
        WorkShopPool pool = new WorkShopPool();
        pool.setRefreshTime(poolMsg.getRefreshTime());
        List<WorkShopItem> list = new ArrayList<>();
        for (WingRoomProto.WorkShopItemTemp workShopItemTemp : poolMsg.getWorkShopItemList()) {
            list.add(parseWorkShopItem(workShopItemTemp));
        }
        pool.setWorkShopItemList(list);
        return pool;

    }

    public static WorkShopItem parseWorkShopItem(WingRoomProto.WorkShopItemTemp temp) {
        WorkShopItem item = new WorkShopItem();
        item.setFurnitureId(temp.getFurnitureId());
        item.setCount(temp.getCount());
        return item;
    }

    public static WingRoomProto.UserWorkShopDataSyncMsg.Builder buildUserWorkShopDataSyncMsg(UserWorkShopData data, int freshDays) {
        WingRoomProto.UserWorkShopDataSyncMsg.Builder syncMsg = WingRoomProto.UserWorkShopDataSyncMsg.newBuilder();
        syncMsg.setPool(buildWorkShopPoolTemp(data.getWorkShopPool(), freshDays));
        for (WorkShopBuyCount buyCount : data.getBuyCountMap().values()) {
            syncMsg.addBuyCount(buildWorkShopBuyCountTemp(buyCount));
        }
        syncMsg.setRefreshTimes(data.getRefreshTimes());
        return syncMsg;
    }

    public static WingRoomProto.VisitPatronsTempMsg.Builder buildVisitPatronsTemp(UserPatrons patrons) {
        WingRoomProto.VisitPatronsTempMsg.Builder patronsTemp = WingRoomProto.VisitPatronsTempMsg.newBuilder();
        patronsTemp.setAbility(patrons.getAbility().toString());
        patronsTemp.setLevel(patrons.getLevel());
        patronsTemp.setPatronsId(patrons.getPatronsId());
        patronsTemp.setPromotionId(patrons.getShowPromotionId());
        patronsTemp.setSkinId(patrons.getSkinId());
        patronsTemp.setStageLv(patrons.getStageLv());
        return patronsTemp;
    }

    public static WingRoomProto.VisitPatronsTempMsg.Builder buildVisitPatronsTemp(WingRoomVisitPatrons patrons) {
        WingRoomProto.VisitPatronsTempMsg.Builder patronsTemp = WingRoomProto.VisitPatronsTempMsg.newBuilder();
        patronsTemp.setAbility(patrons.getAbility().toString());
        patronsTemp.setLevel(patrons.getLevel());
        patronsTemp.setPatronsId(patrons.getPatronsId());
        patronsTemp.setPromotionId(patrons.getPromotionId());
        patronsTemp.setSkinId(patrons.getSkinId());
        patronsTemp.setStageLv(patrons.getStageLv());
        return patronsTemp;
    }

    public static WingRoomProto.UserVisitPatronsSync.Builder buildUserVisitPatronsSync(UserPatronsVisitData patronsVisitData) {
        WingRoomProto.UserVisitPatronsSync.Builder sync = WingRoomProto.UserVisitPatronsSync.newBuilder();
        sync.setState(patronsVisitData.getState());
        if(patronsVisitData.getReceiveTime() > 0){
            sync.setDoneTime(patronsVisitData.getDoneTime() );
        }
        sync.setVisitor(patronsVisitData.getVisitor());
        if(patronsVisitData.getUserBaseInfo() != null) {
            sync.setBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(patronsVisitData.getUserBaseInfo()));
        }
        for (WingRoomVisitPatrons patrons : patronsVisitData.getPatronsList()) {
            sync.addPatrons(WingRoomPb.buildVisitPatronsTemp(patrons));
        }
        return sync;
    }
}
